package cn.abcsys.cloud.devops.pipeline.builder.config;

import cn.abcsys.cloud.devops.pipeline.common.JenkinsItem;
import cn.abcsys.cloud.devops.pipeline.common.Status;

public class JenkinsJobXmls {
	public static String jobxml;
	public static String getJobxml(JenkinsItem jenkinsItem){
		String postbuilders = getPostbuilders(jenkinsItem);
		jobxml= "<?xml version='1.0' encoding='UTF-8'?>                                               "+
				"<maven2-moduleset plugin=\"maven-plugin@2.15.1\">                                   "+
				"  <description></description>                                                       "+
				"  <keepDependencies>false</keepDependencies>                                        "+
				"  <properties>"+
				"      <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin=\"gitlab-plugin@1.4.5\">"+
				"        <gitLabConnection></gitLabConnection>"+
				"      </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>"+
				"      <jenkins.model.BuildDiscarderProperty>"+
				"        <strategy class=\"hudson.tasks.LogRotator\">"+
				"          <daysToKeep>"+jenkinsItem.getJenkinsDaysToKeep()+"</daysToKeep>"+
				"          <numToKeep>"+jenkinsItem.getJenkinsNumToKeep()+"</numToKeep>"+
				"          <artifactDaysToKeep>-1</artifactDaysToKeep>"+
				"          <artifactNumToKeep>-1</artifactNumToKeep>"+
				"        </strategy>"+
				"      </jenkins.model.BuildDiscarderProperty>"+
				"  </properties>"+
				"  <scm class=\"hudson.plugins.git.GitSCM\" plugin=\"git@3.3.0\">                    "+
				"    <configVersion>2</configVersion>                                                "+
				"    <userRemoteConfigs>                                                             "+
				"      <hudson.plugins.git.UserRemoteConfig>                                         "+
				"        <url>"+jenkinsItem.getRepository_url()+"</url>                              "+
				"      </hudson.plugins.git.UserRemoteConfig>                                        "+
				"    </userRemoteConfigs>                                                            "+
				"    <branches>                                                                      "+
				"      <hudson.plugins.git.BranchSpec>                                               "+
				"        <name>"+jenkinsItem.getBranch_specifier()+"</name>                          "+
				"      </hudson.plugins.git.BranchSpec>                                              "+
				"    </branches>                                                                     "+
				"    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>    "+
				"    <submoduleCfg class=\"list\"/>                                                  "+
				"    <extensions/>                                                                   "+
				"  </scm>                                                                            "+
				"  <canRoam>true</canRoam>                                                           "+
				"  <disabled>false</disabled>                                                        "+
				"  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>        "+
				"  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>            "+
				"  <triggers/>                                                                       "+
				"  <concurrentBuild>false</concurrentBuild>                                          "+
				"  <aggregatorStyleBuild>true</aggregatorStyleBuild>                                 "+
				"  <incrementalBuild>false</incrementalBuild>                                        "+
				"  <ignoreUpstremChanges>false</ignoreUpstremChanges>                                "+
				"  <ignoreUnsuccessfulUpstreams>false</ignoreUnsuccessfulUpstreams>                  "+
				"  <archivingDisabled>false</archivingDisabled>                                      "+
				"  <siteArchivingDisabled>false</siteArchivingDisabled>                              "+
				"  <fingerprintingDisabled>false</fingerprintingDisabled>                            "+
				"  <resolveDependencies>false</resolveDependencies>                                  "+
				"  <processPlugins>false</processPlugins>                                            "+
				"  <mavenValidationLevel>-1</mavenValidationLevel>                                   "+
				"  <runHeadless>false</runHeadless>                                                  "+
				"  <disableTriggerDownstreamProjects>false</disableTriggerDownstreamProjects>        "+
				"  <blockTriggerWhenBuilding>true</blockTriggerWhenBuilding>                         "+
				"  <settings class=\"jenkins.mvn.DefaultSettingsProvider\"/>                         "+
				"  <globalSettings class=\"jenkins.mvn.DefaultGlobalSettingsProvider\"/>             "+
				"  <reporters/>                                                                      "+
				"  <publishers/>                                                                     "+
				"  <buildWrappers/>                                                                  "+
				"  <prebuilders/>                                                                    "+
				postbuilders+
				"  <runPostStepsIfResult>                                                            "+
				"    <name>FAILURE</name>                                                            "+
				"    <ordinal>2</ordinal>                                                            "+
				"    <color>RED</color>                                                              "+
				"    <completeBuild>true</completeBuild>                                             "+
				"  </runPostStepsIfResult>                                                           "+
				"</maven2-moduleset>                                                                 ";
		return jobxml;
	}
	public static String getPostbuilders(JenkinsItem jenkinsItem){
		StringBuffer postbuilders = new StringBuffer();
		postbuilders.append("<postbuilders>");
		String makeImageBuilder = "	<org.jenkinsci.plugins.dockerbuildstep.DockerBuilder plugin=\"docker-build-step@1.43\">"+
							"	  <dockerCmd class=\"org.jenkinsci.plugins.dockerbuildstep.cmd.CreateImageCommand\">"+
							//$WORKSPACE/docker
							"	    <dockerFolder>"+jenkinsItem.getDockerfile_dir()+"</dockerFolder>"+
							//cicd_demo_$BUILD_NUMBER
							"	    <imageTag>"+jenkinsItem.getImageName()+"</imageTag>"+
							"	    <dockerFile>Dockerfile</dockerFile>"+
							"	    <noCache>false</noCache>"+
							"	    <rm>false</rm>"+
							"	    <buildArgs></buildArgs>"+
							"	  </dockerCmd>"+
							"	</org.jenkinsci.plugins.dockerbuildstep.DockerBuilder>";
		String pushImageBuilder ="	<org.jenkinsci.plugins.dockerbuildstep.DockerBuilder plugin=\"docker-build-step@1.43\">"+
							"	  <dockerCmd class=\"org.jenkinsci.plugins.dockerbuildstep.cmd.PushImageCommand\">"+
							"	    <dockerRegistryEndpoint plugin=\"docker-commons@1.6\">"+
							//http://42.159.118.183:9050
							"	      <url>"+jenkinsItem.getRegistry_url()+"</url>"+
							"	        </dockerRegistryEndpoint>"+
							//cicd_demo_$BUILD_NUMBER
							"	        <image></image>"+
							//latest
							"	        <tag></tag>"+
							"	        <registry></registry>"+
							"	      </dockerCmd>"+
							"	    </org.jenkinsci.plugins.dockerbuildstep.DockerBuilder>"+
							"	  ";
		if(jenkinsItem.getMakeImage()==Status.JUDGE.YES.ordinal()){
			postbuilders.append(makeImageBuilder);
			if(jenkinsItem.getPushImage()==Status.JUDGE.YES.ordinal()){
				postbuilders.append(pushImageBuilder);
			}
		}
		postbuilders.append("<hudson.tasks.Shell>");
		postbuilders.append("<command>"+jenkinsItem.getCommand()+"</command>");
		postbuilders.append("</hudson.tasks.Shell>");
		postbuilders.append("</postbuilders>");
		return null;
	}
}
